package com.emep.changzhi.analyse.service.statistics;

import com.emep.changzhi.analyse.entity.aqi.DataAqiInfo;
import com.emep.changzhi.analyse.repository.aqi.DataAqiInfoRepository;
import com.emep.changzhi.analyse.service.outlet.SysOutletInfoService;
import com.emep.changzhi.analyse.utils.DateTimeUtil;
import com.emep.changzhi.analyse.utils.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;


@Service
public class PollutionAverageService {

    @Autowired
    private DataAqiInfoRepository dataAqiInfoRepository;
    private static final Logger logger = LoggerFactory.getLogger(SysOutletInfoService.class);


    public Object findapiByTime(Map<String, Object> payload) {

            List<DataAqiInfo> dataAqiInfoList = new ArrayList<DataAqiInfo>();
            float sum = 0.0f;
            int count = 0;

            DecimalFormat df = new DecimalFormat("0.00");
            //行政区划
            String cityCode = payload.get("cityCode") != null ? payload.get("cityCode").toString() : "";
            //开始时间
            String startDate = payload.get("startDate") != null ? payload.get("startDate").toString() : "";
            //结束时间
            String endDate = payload.get("endDate") != null ? payload.get("endDate").toString() : "";
            //污染物名称
            String pollutionName = String.valueOf(payload.get("pollutionName") != null ? payload.get("pollutionName").toString() : "");
            //站点名称
            String siteName = String.valueOf(payload.get("siteName") != null ? payload.get("siteName").toString() : "");
        try {
            if (!StringUtils.isEmpty(cityCode) && !StringUtils.isEmpty(startDate) && !StringUtils.isEmpty(endDate) && !StringUtils.isEmpty(pollutionName)) {
                //开始时间
                String start=String.valueOf( DateTimeUtil.stringToDate(startDate,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());
                //结束时间
                String end= String.valueOf( DateTimeUtil.stringToDate(endDate,DateTimeUtil.DATETIME_DEFAULT_FORMAT).getTime());

                dataAqiInfoList = dataAqiInfoRepository.findAll(new Specification<DataAqiInfo>() {
                    @Override
                    public javax.persistence.criteria.Predicate toPredicate(Root<DataAqiInfo> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
                        javax.persistence.criteria.Predicate p1 = null;
                        javax.persistence.criteria.Predicate p2 = null;
                        javax.persistence.criteria.Predicate p3 = null;
                        javax.persistence.criteria.Predicate p4 = null;


                        //大于或等于传入时间
                        p1 = criteriaBuilder.greaterThanOrEqualTo(root.get("timePoint").as(String.class), start);
                        //小于于或等于传入时间
                        p2 = criteriaBuilder.lessThanOrEqualTo(root.get("timePoint").as(String.class), end);
                        //行政区划
                        p3 = criteriaBuilder.equal(root.get("areaId").as(String.class), cityCode);
//
//
                        //站点名称
                        p4 = criteriaBuilder.equal(root.get("positionName").as(String.class), siteName);
                        query.where(criteriaBuilder.and(p1, p2, p3, p4));
                        return query.getRestriction();
                    }
                });
                if (dataAqiInfoList != null) {
                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getCo());
                        sum += c;
                    }
                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getNo2());
                        sum += c;
                    }
                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getO3());
                        sum += c;
                    }
                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getPm10());
                        sum += c;
                    }

                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getPm25());
                        sum += c;
                    }
                    for (DataAqiInfo d : dataAqiInfoList) {
                        float c = Float.valueOf(d.getSo2());
                        sum += c;
                    }
                    count = dataAqiInfoList.size();


                }

            }
        } catch (Exception e)

        {
            logger.error(e.getLocalizedMessage());
        }
            return df.format(sum / count);

        }


}